<template>
  <div>
    <Navbar :appTitle="appTitle"/>
    <div
      class="form-content wrap-apply"
      :style="{
        top: GLOBAL.currentEnvironment ? '0' : '',
        height: GLOBAL.currentEnvironment ? '100vh' : 'calc(100vh - 100px)',
        paddingBottom: GLOBAL.currentEnvironment ? '120px' : '',
      }"
    >
      <van-cell-group class="formItemFlex">
        <p class="label required FormTit"><span></span>盘点单编号</p>
        <div class="haveUnitBox">
          <van-field
            v-model="buildForm.taskCode"
            :maxLength="50"
            placeholder="请输入、不输入则自动生成"
            input-align="right"
          />
        </div>
      </van-cell-group>
      <div class="bottomLine"></div>
      <van-cell-group  class="formItemFlex">
        <p class="label required FormTit"><span></span>单据日期</p>
        <div class="haveUnitBox">
          <van-field
            input-align="right"
            is-link
            readonly
            name="picker"
            v-model="buildForm.docDate"
            placeholder="请选择"
          />
        </div>
      </van-cell-group>
      <div class="bottomLine"></div>
      <van-cell-group  class="formItemFlex">
        <p class="label required FormTit"><span></span>盘点范围维度</p>
        <van-field-select-picker
          :supportClear="true"
          label=""
          placeholder="请选择"
          v-model="buildForm.inventoryDimension"
          :columns="warehousingTypes"
          :option="{ label: 'text', value: 'value' }"
          @confirm="confirm"
          @change="change"
        />
      </van-cell-group>
      <van-cell-group  class="formItemFlex">
        <p class="label required  FormTit"><span></span>盘点范围</p>
        <div class="haveUnitBox">
          <van-field
            v-model="buildForm.inventoryScopeText"
            input-align="right"
            placeholder="请选择"
            readOnly
            @click="showCheckedModel"
          />
        </div>
      </van-cell-group>
      <div class="bottomLine"></div>
      <van-cell-group  class="formItemFlex">
        <p class="label required FormTit"><span></span>明细显示</p>
        <van-field-select-picker
          :supportClear="true"
          label=""
          placeholder="请选择"
          v-model="buildForm.detailsShow"
          :columns="detailsShow"
          :option="{ label: 'text', value: 'value' }"
        />
      </van-cell-group>
      <div class="bottomLine"></div>
      
      <div class="search-bottoms">
        <van-button class="btnconfirm" :loading="subLoading" loading-text="提交中..." @click="btnConfirm">去盘点</van-button>
      </div>
    </div>
    <!--选择库位 -->
    <binLocation-pickers-ak
      v-if="isShowCheckedModel"
      :listUrl="usedUrl"
      :columns="usedColumn"
      :checkedShow="usedcheckedShow"
      :usedMethods="usedGetMethods"
      :selectedGoods="usedcheckedShow ? checkedList : []"
      :usedOutBox="usedOutBox"
      :placeholder="usedPlaceholder"
      :idkey="'id'"
      @getCheckedValue="getCheckedValue"
      @handleList="handleList"
    ></binLocation-pickers-ak>
  </div>
</template>
<script>
import moment from "moment";
import Navbar from "@/components/navBar/navBar_right";
import VanFieldSelectPicker from "@/components/ActionSheet/VanFieldSelectPicker";
import {
  genEntityCode,
  getDictItemsByTypes,
  addItask,
} from "@/api/inventoryOrder";
import store from "@/store";
import BinLocationPickersAk from "@/components/choiceuse/BinLocationPickersAk";

export default {
  name: "inventoryRulesForm",
  components: {
    Navbar,
    VanFieldSelectPicker,
    BinLocationPickersAk,
  },
  data() {
    return {
      appTitle: "盘点规则",
      buildForm: {
        taskCode: '',
        docDate: "",
        inventoryDimension: "",
        inventoryScope: "",
        inventoryScopeText: "",
        detailsShow: "",
      },

      warehousingTypes: [],
      detailsShow: [
        {value: 'BLANK', text: '空白盘点单'},
        {value: 'GOODS', text: '显示物品'},
      ],

      isShowCheckedModel: false,
      usedGetMethods: false,
      usedUrl: '',
      usedColumn: {},
      usedcheckedShow: false,
      usedOutBox: false,
      usedPlaceholder: '',
      checkedList: [],

      warehouseUrl: "/baseSetting/warehouse/list",
      warehouseColumns: {
        titleKey: ["applyNumber", "name"], //标题
        content: [
          //内容
          {
            name: "仓库分类",
            key: ["typeText"],
          },
          {
            name: "库位数",
            key: ["latticeNumbers"],
          },
        ],
        statusKey: ['statusText'], //标签
      },

      warehouseAreaUrl: "/baseSetting/warehouseArea/pageList",
      warehouseAreaColumns: {
        titleKey: ["areaCode", "areaName"], //标题
        content: [
          //内容
          {
            name: "仓库名称",
            key: ["warehouseName"],
          },
          {
            name: "库区库位",
            key: ["latticeNum"],
          },
        ],
        statusKey: ['statusText'], //标签
      },

      latticeUrl: "/baseSetting/warehouseLattice/pageList",
      latticeColumns: {
        titleKey: ["latticeCode", "latticeName"], //标题
        content: [
          //内容
          {
            name: "排列层",
            key: ["locationInfo"],
          },
          {
            name: "库区名称",
            key: ["areaName"],
          },
          {
            name: "仓库名称",
            key: ["warehouseName"],
          },
        ],
        statusKey: ['statusText'], //标签
      },
      subLoading: false
    };
  },
  beforeRouteLeave(to, from, next) {
    if (this.isShowCheckedModel) {
      this.isShowCheckedModel = false;
      this.appTitle = '盘点规则';
      next(false);
    } else {
      next();
    }
  },
  async created() {
    store.commit("SET_CURRENT_PREFIX", "/jksaas-produce/produce");
    this.initData();
  },
  methods: {
    // 确认选中 多选 库区、库位
    async getCheckedValue(list) {
      if(!list || list.length == 0) {
        return
      }
      this.isShowCheckedModel = false;
      this.appTitle = "盘点规则";

      this.checkedList = list
      if(this.buildForm.inventoryDimension == 'AREA') { // 库区
        let areaNameList = []
        let areaIdList = []

        list.map((ele) => {
          areaNameList.push(ele.areaName)
          areaIdList.push(ele.id)
        })

        this.buildForm.inventoryScopeText = areaNameList.join()
        this.buildForm.inventoryScope = areaIdList.join()
      } else if(this.buildForm.inventoryDimension == 'LATTICE') { // 库位
        let latticeNameList = []
        let latticeIdList = []

        list.map((ele) => {
          latticeNameList.push(ele.latticeName)
          latticeIdList.push(ele.id)
        })
        
        this.buildForm.inventoryScopeText = latticeNameList.join()
        this.buildForm.inventoryScope = latticeIdList.join()
      }
    },
    // 确认选中 单选 仓库
    handleList(binLocationid, item, text) {
      if(!item) {
        return
      }

      this.isShowCheckedModel = false;
      this.appTitle = '盘点规则';
      this.buildForm.inventoryScope = item.id
      this.buildForm.inventoryScopeText = item.name
    },
    confirm(value, index, resultValue) {
      if(!value) {
        this.buildForm.inventoryScopeText = ''
        this.buildForm.inventoryScope = ''
        this.checkedList = []
        return
      }
    },
    change(value, index, resultValue) {
      if(value) {
        this.buildForm.inventoryScopeText = ''
        this.buildForm.inventoryScope = ''
        this.checkedList = []
        return
      }
    },
    // 仓库 库区 库位选择
    showCheckedModel() {
      if(this.buildForm.inventoryDimension == 'WAREHOUSE') {
        this.appTitle = '仓库';
        this.usedUrl = this.warehouseUrl
        this.usedColumn = this.warehouseColumns
        this.usedcheckedShow = false
        this.usedGetMethods = false
        this.usedOutBox = false
        this.usedPlaceholder = '仓库编号|仓库名称'
      } else if(this.buildForm.inventoryDimension == 'AREA') {
        this.appTitle = '库区';
        this.usedUrl = this.warehouseAreaUrl
        this.usedColumn = this.warehouseAreaColumns
        this.usedcheckedShow = true
        this.usedGetMethods = false
        this.usedOutBox = false
        this.usedPlaceholder = '库区编号|库区名称|仓库名称'
      } else if(this.buildForm.inventoryDimension == 'LATTICE') {
        this.appTitle = '库位';
        this.usedUrl = this.latticeUrl
        this.usedColumn = this.latticeColumns
        this.usedcheckedShow = true
        this.usedGetMethods = false
        this.usedOutBox = true
        this.usedPlaceholder = '库位编号|库位名称'
      } else {
        this.appTitle = '盘点规则';
        this.usedUrl = ''
        this.usedColumn = {}
        this.usedcheckedShow = false
        this.usedGetMethods = true
        this.usedOutBox = true
        this.usedPlaceholder = ''
        return
      }

      this.isShowCheckedModel = true
    },

    // 提交
    btnConfirm() {
      let {taskCode, docDate, inventoryDimension, inventoryScope, detailsShow} = this.buildForm

      try {
        if(!taskCode || taskCode == ''){
          throw new Error(`请输入盘点单编号`);
        }
        if(!docDate || docDate == ''){
          throw new Error(`请选择单据日期`);
        }
        if(!inventoryDimension || inventoryDimension == ''){
          throw new Error(`请选择盘点范围维度`);
        }
        if(!inventoryScope || inventoryScope == ''){
          throw new Error(`请选择盘点范围`);
        }
        if(!detailsShow || detailsShow == ''){
          throw new Error(`请选择明细显示`);
        }
      } catch (error) {
        this.$toast(error.message);
        return false;
      }

      this.handleSubmit();
    },
    handleSubmit() {
      let {taskCode, docDate, inventoryDimension, inventoryScope, detailsShow} = this.buildForm

      let params = {
        taskCode: taskCode,
        docDate: docDate,
        inventoryDimension: inventoryDimension,
        inventoryScope: inventoryScope,
        detailsShow: detailsShow,
      };
      this.subLoading = true
      addItask(params).then((res) => {
        if (res.success) {
          this.$router.push({
            path: "/inventoryItaskForm",
            query: {
              id: res.result.id
            }
          });
        } else {
          this.$toast(res.message);
        }
        this.subLoading = false
      }).catch(() => {
        this.subLoading = false
      }).finally(() => {
        this.subLoading = false
      })
    },
 
    // 初始化页面数据
    initData() {
      this.appTitle = "盘点规则";
      // 盘点单编号
      genEntityCode({ entityCode: "WMS_WAREHOUSE_INVENTORY",}).then((res) => {
        if (res.code == 200) {
          this.buildForm.taskCode = res.result;
        } else {
          this.buildForm.taskCode = "";
          this.$toast(res.message);
        }
      });

      // 盘点范围维度 List
      getDictItemsByTypes({type: 6}).then((res) => {
        if (res.code == 200 && res.result) {
          this.warehousingTypes = res.result
        }
      });

      // 单据日期 默认选中当天 不可变更
      this.buildForm.docDate = moment(new Date()).format("YYYY-MM-DD");
      // 盘点范围维度 默认选中仓库选项
      this.buildForm.inventoryDimension = 'WAREHOUSE'
      // 明细显示 默认选中空白盘点单
      this.buildForm.detailsShow = 'BLANK'
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/theme.scss";
@import "@/views/workManagement/materialRequisition/materialForm.scss";
.tabs-contain {
  position: relative;

  /deep/.van-tabs__nav--line.van-tabs__nav--complete {
    width: calc(100% - 120px);
  }
  /deep/.van-tabs__wrap--scrollable .van-tab {
    width: 33%;
    min-width: 230px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .all-down-btn {
    position: absolute;
    top: 0;
    right: 0;
    width: 120px;
    height: 90px;
    font-size: 28px;
    color: rgb(25, 137, 250);
    line-height: 90px;
    text-align: center;
    background-color: #fff;
    /deep/.van-icon {
      top: 6px;
    }
  }
  .all-down-model {
    position: absolute;
    top: 0;
    right: 0;
    width: 100vw;
    background-color: #fff;
    z-index: 9;
    border-bottom: 1px solid #f3f3f3;
    padding: 6px;
    .tags-contain {
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 10px;
      .goodsNameTag {
        width: 22%;
        margin: 10px 10px;
        padding: 5px 20px;
        background-color: #f3f3f3;
        color: #333333;
        border-radius: 5px;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
        text-align: center;
        height: 60px;
        line-height: 42px;
      }
    }
    .ctrBtn-contian {
      height: 50px;
      text-align: center;
      color: #1890ff;
      font-size: 24px;
      line-height: 50px;
      /deep/.van-icon {
        top: 6px;
      }
    }
  }
}
.qrcodeModel{
  .nav_contain {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #ceced1;
    padding: 24px 20px 10px;
    span {
      font-size: 32px;
    }
    .left {
      color: #3ba1ff;
    }
    .center {
      color: #191f25;
      font-weight: bolder;
    }
    .right {
      color: #3ba1ff;
    }
  }
  .list {
    .wrap {
      background: #fff;
      width: 100vw;
      .otherText {
        display: flex; 
        justify-content: space-between;
        font-size: 32px;
        color: #191f25;
        margin: 0;
        font-weight: 500;
        .deleteStyle {
          font-weight: 400; 
          color: #ff4d4f
        }
      }
      .top {
        display: flex;
        justify-content: flex-start;
        padding-top: 10px;
        .icon {
          width: 6px;
          height: 32px;
          background: #1890ff;
          margin: 6px 10px 0 10px;
        }
        .title {
          width: 602px;
          margin-right: 20px;
          font-size: 30px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #191f25;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .titleWidth {
          width: 600px;
          margin: 0 25px;
        }
      }
      .center {
        margin: 0 0 30px;
        p {
          margin: 6px 25px;
          font-size: 28px;
          display: flex;
          position: relative;
          .key {
            color: #888888;
          }
          .value {
            color: #191f25;
            width: 380px;
            display: block;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          .arrow {
            color: #1890ff;
            position: absolute;
            right: -10px;
          }
          /deep/.van-tag {
            margin-right: 10px;
            width: 160px;
            flex-direction: column;
          }
        }
      }
    }
  }
}

.form-content {
  width: 100%;
  height: calc(100vh - 203px);
  background: #f5f5f5;
  overflow: scroll;
  padding: 20px 0;
  .formgroup {
    background: #ffffff;
    margin-top: 0.1rem;
    margin-bottom: 0.2rem;
    padding-top: 0.1rem;

    .title {
      width: 0.58rem;
      height: 0.14rem;
      font-size: 0.15rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #191f25;
      line-height: 0.05rem;
      margin-left: 0.16rem;
    }
  }
  .search-bottoms {
    width: 100%;
    height: 100px;
    background: #ffffff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    bottom: 0px;
    padding: 0rem 11px;
    box-sizing: border-box;
    font-size: 32px;
    font-weight: 500;
    z-index: 9;
    .btnconfirm {
      flex: 1;
      background: #1890ff;
      box-sizing: border-box;
      height: 78px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 6px;
      color: #ffffff;
    }
    .btncancle {
      width: 270px;
      margin: 0 30px;
      background: #f2f2f6;
      color: #1a2026;
      box-sizing: border-box;
      height: 78px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 6px;
    }
  }
}
.bottomLine {
  height: 1px;
  background-color: #f2f2f2;
  margin: 0 2px;
}
.haveUnitBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding-right: 32px;
  /deep/ .van-cell {
    padding-right: 0;
    width: 600px;
  }
  /deep/ .van-field__control {
    max-width: 700px;
    // text-align: right;
  }
}
/deep/ .van-nav-bar {
  z-index: 99;
}
/deep/.van-sidebar-item--select::before {
  color: #1890ff;
  background-color: #1890ff;
}
/deep/.van-tree-select__item--active {
  color: #1890ff;
}
.addGoodScontain {
  display: flex;
  flex-wrap: nowrap;
  padding: 16px 0;
  .addgoods {
    flex: 1;
    background: #fff;
    font-size: 32px;
    text-align: center;
    height: 78px;
    line-height: 78px;
    color: #1890ff;
  }
}
.required span::after {
  left: -20px;
  top: 3px;
}
.filedClear {
  width: 28px;
  height: 28px;
}
.formItemFlex {
  display: flex;
  flex-direction: row;
  .FormTit {
    flex: 1;
    min-width: 3.3rem;
    padding-top: 0.33rem;
  }
  .haveUnitBox {
    flex: 1;
    .van-cell {
      width: 6.2rem;
    }
  }
}
.list {
  margin-top: 10px;
  .wrap {
    background: #fff;
    width: 100vw;
    .otherText {
      display: flex; 
      justify-content: space-between;
      font-size: 32px;
      font-weight: bold;
      color: #191f25;
      .deleteStyle {
        font-weight: 400; 
        color: #ff4d4f
      }
    }
    .top {
      display: flex;
      justify-content: flex-start;
      padding-top: 10px;
      .icon {
        width: 6px;
        height: 32px;
        background: #1890ff;
        margin: 6px 10px 0 10px;
      }
      .title {
        width: 602px;
        margin-right: 20px;
        font-size: 30px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #191f25;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .titleWidth {
        width: 600px;
        margin: 16px 25px;
      }
    }
    .center {
      margin: 0 0 30px;
      p {
        margin: 6px 25px;
        font-size: 28px;
        display: flex;
        position: relative;
        .key {
          color: #888888;
        }
        .value {
          color: #191f25;
          width: 380px;
          display: block;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .arrow {
          color: #1890ff;
          position: absolute;
          right: -10px;
        }
        /deep/.van-tag {
          margin-right: 10px;
          width: 160px;
          flex-direction: column;
        }
      }
      .showAll {
        background: #f5f8ff;
        width: 100vw;
        margin-left: -30px;
        padding: 30px;
        margin-bottom: 30px;
      }
      .showAll2 {
        width: 100%;
        margin: 0 0 30px 0;
        padding: 30px 0;
      }
    }

  }
}
.d-head {
  width: 100%;
  padding: 32px;
  background: #ffffff;
  margin-bottom: 10px;
  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      flex: 1;
      .title {
        font-size: 32px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #191f25;
        line-height: 32px;

        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        -o-text-overflow: ellipsis;
      }
      .row {
        font-size: 28px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #888888;
        margin-top: 20px;
      }
    }
    .right {
      text-align: right;
      font-size: 26px;
      margin-top: 72px;
      .row {
        padding-top: 0px;
      }
      .warining {
        color: #fe9400;
      }
      .success {
        color: #4bd863;
      }
    }
  }
}
</style>
